package com.smartcruise.util.customView;

import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.smartcruise.R;


/**
 * modified by FengChaoQun on 2017/6/27 0027 17:24
 * description:优化代码
 * 正在加载对话框
 */
public class LoadingDialog extends Dialog {

    private Context mContext;
    private TextView loadText;
    private onDismiss onDismiss;
    private ObjectAnimator objectAnimator;
    private ImageView dialogImage;
    private View rootView;

    public LoadingDialog(Context context) {
        super(context, R.style.Dialog);

        this.mContext = context;

        rootView = View.inflate(mContext, R.layout.loadingdialog, null);
        loadText = (TextView) rootView.findViewById(R.id.loading_text);
        dialogImage = (ImageView) rootView.findViewById(R.id.dialogImage);
        setContentView(rootView);

        //实现转动
        objectAnimator = ObjectAnimator.ofFloat(dialogImage, "rotation", 0f, 360f);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.setDuration(1000);
        objectAnimator.setRepeatCount(-1);

        this.setCanceledOnTouchOutside(false);
    }

    @Override
    public void show() {
        super.show();
        objectAnimator.start();

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = mContext.getResources().getDimensionPixelSize(R.dimen.loadingDialogWidth); //设置宽度
        lp.height = mContext.getResources().getDimensionPixelSize(R.dimen.loadingDialogWidth); //设置宽度
        getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        if (onDismiss != null) {
            onDismiss.doBeforeDismiss();
        }
        objectAnimator.cancel();
        super.dismiss();
    }

    public void setOnDismiss(onDismiss on) {
        this.onDismiss = on;
    }

    public void setLoadText(String content) {
        loadText.setText(content);
    }

    public interface onDismiss {
        void doBeforeDismiss();
    }
}